<?php

use think\facade\Db;
use think\app;

/**
 * @Description    自定义的非常用函数
 * @Copyright      Copyright (c) 山东艾思网络科技有限公司(https://www.aisisofit.cn)
 * @Date           2022/2/28
 * @CreateBy       VS Code
 */


/**
 * 查询出department下的所有子结点(下属职位)ID by zhaojiping
 * @param string $pids 以,号分隔开的department的pid
 * @return array 本身和所有的下属职位ID
 */
function get_department_ids($department_pids) {
    static $str; // 定义一个静态变量, 存储嵌套时的变量
    $str .= $department_pids . ',';
    $ids = Db::name('CoreDepartment')->cache(3)->where('pid', 'in', $department_pids)->column('id');
    if ($ids) {
        $ids = implode(',', $ids); // 数组转化为字符串
        get_department_ids($ids);
    }
    return explode(',', $str); // 转化为数组输出
}

/**
 * 根据员工组织架构ID, 获取员工ID by zhaojiping
 * @param int $user_id 用户ID
 * @return array 用户所有员工ID
 */
function get_sub_department_user_ids($department_id) {
    $department_ids = get_department_ids($department_id);
    $uids = Db::name('CoreUser')->cache(3)->where('department_id', 'in', $department_ids)->column('id');
    return $uids;
}
